home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-07 | 8.8 KB | 279 lines |
- /*
- PC Plus sample Java application.
- Demonstrates how to transfer items between two Vectors by
- boxes by double-clicking List boxes. Implements a basic
- 'shopping list' manager.
- */
-
- import java.awt.*;
- import java.util.*; // Class Vector is in java.util
-
-
- public class Frame1 extends Frame {
-
- // Declare two Vectors to main a stock list and a list of items purchased
- Vector thingsInRoom;
- Vector thingsInSwagBag;
- // Note, these Vectors are not actually available for use until they are
- // created with 'new'. In this project, this is done in the Frame's 'show'
- // method. However, the Vectors could have been created at the time of declaration
- // using the following statements instead of the declarations above:
- // Vector thingsInRoom = new Vector();
- // Vector thingsInSwagBag = new Vector();
-
-
- void MoveObFromV1ToV2(Object Ob, Vector V1, Vector V2 ) {
- // Move an item from Vector V1 to Vector V2
- V2.addElement(Ob);
- V1.removeElement(Ob);
- }
-
-
- void PlayerList_DblClick(Event event) {
- // transfer selected item from thingsInSwagBag vector to thingsInRoom vector
- // and display the change in the list boxes
- Object thing = thingsInSwagBag.elementAt(PlayerList.getSelectedIndex());
- MoveObFromV1ToV2(thing, thingsInSwagBag, thingsInRoom );
- UpdateListBoxes();
-
- }
-
- void RoomList_DblClick(Event event) {
- // transfer selected item from thingsInRoom vector to thingsInSwagBag vector
- // and display the change in the list boxes
- Object thing = thingsInRoom.elementAt(RoomList.getSelectedIndex());
- MoveObFromV1ToV2(thing, thingsInRoom, thingsInSwagBag );
- UpdateListBoxes();
- }
-
- void UpdateListBox( List l, Vector v) {
- // Enumerate through the elements in Vector v and add them,
- // as strings, to the List l
- l.clear();
- for (Enumeration e = v.elements(); e.hasMoreElements(); ) {
- // Cast element to Thing and call .getName method to retrieve the name String
- l.addItem(((Thing)e.nextElement()).getName());
- }
- }
-
- void UpdateListBoxes() {
- // Call UpdateListBox with the Lists and the Vectors representing
- // the things in the Basket and the things in Stock
- UpdateListBox(PlayerList, thingsInSwagBag);
- UpdateListBox(RoomList, thingsInRoom);
- }
-
- /* The following methods are used to display a description of the Thing selected in each of the
- two list boxes. They are, functionally, identical. The first method (RoomList_ListSelect)
- is the more compact of the two as it performs everything in one line. However, the convoluted
- syntax makes it extremely difficult to read. Compare this with the sedond method
- (PlayerList_ListSelect) n which a variable, 't' of type Thing, has been introduced purely
- to make the code a bit easier to read! */
-
-
- void RoomList_ListSelect(Event event) {
- // When an item in the RoomList box is selected, display its description
- textField1.setText(((Thing)thingsInRoom.elementAt(RoomList.getSelectedIndex())).getDescription());
- }
-
- void PlayerList_ListSelect(Event event) {
- // When an item in the PlayerList box is selected, display its description
- // Since getDescription is a method of Thing, the selected Vector element
- // must be explicitly cast to the type Thing.
- Thing t;
- t = (Thing)thingsInSwagBag.elementAt(PlayerList.getSelectedIndex());
- textField1.setText( t.getDescription() );
- }
-
- void Open_Action(Event event) {
- OpenFileDialog.show();
- }
-
- void About_Action(Event event) {
- (new AboutDialog(this, "About...", false)).show();
- }
-
- void Exit_Action(Event event) {
- (new QuitDialog(this, "Quit the Application?", false)).show();
- }
-
- public Frame1() {
-
- //{{INIT_CONTROLS
- setLayout(null);
- addNotify();
- resize(insets().left + insets().right + 457,insets().top + insets().bottom + 398);
- OpenFileDialog = new java.awt.FileDialog(this, "Open",FileDialog.LOAD);
- RoomList = new java.awt.List(0,false);
- add(RoomList);
- RoomList.reshape(insets().left + 14,insets().top + 45,201,225);
- PlayerList = new java.awt.List(0,false);
- add(PlayerList);
- PlayerList.reshape(insets().left + 238,insets().top + 45,201,225);
- label1 = new java.awt.Label("THINGS HERE...");
- label1.reshape(insets().left + 14,insets().top + 15,196,15);
- add(label1);
- label2 = new java.awt.Label("THING YOU HAVE...");
- label2.reshape(insets().left + 238,insets().top + 15,203,15);
- add(label2);
- label3 = new java.awt.Label("DOUBLE-CLICK TO TAKE");
- label3.reshape(insets().left + 14,insets().top + 278,203,22);
- add(label3);
- label4 = new java.awt.Label("DOUBLE-CLICK TO DROP");
- label4.reshape(insets().left + 238,insets().top + 278,203,24);
- add(label4);
- textField1 = new java.awt.TextField();
- textField1.reshape(insets().left + 14,insets().top + 315,429,28);
- add(textField1);
- setTitle("A Basic Application");
- //}}
-
- //{{INIT_MENUS
- mainMenuBar = new java.awt.MenuBar();
-
- menu1 = new java.awt.Menu("File");
- menu1.add("Open...");
- menu1.add("Save");
- menu1.add("Save As...");
- menu1.addSeparator();
- menu1.add("Exit");
- mainMenuBar.add(menu1);
-
- menu2 = new java.awt.Menu("Edit");
- menu2.add("Cut");
- menu2.add("Copy");
- menu2.add("Paste");
- mainMenuBar.add(menu2);
-
- menu3 = new java.awt.Menu("Help");
- menu3.add("About");
- mainMenuBar.add(menu3);
- setMenuBar(mainMenuBar);
- //}}
- }
-
- public Frame1(String title) {
- this();
- setTitle(title);
- }
-
- public synchronized void show() {
- move(50, 50);
- super.show();
-
- // INITIALISATION - runs when form is first shown
- // Create the two Vectors
- thingsInRoom = new Vector();
- thingsInSwagBag = new Vector();
-
- // Add items to thingsInRoom Vector
- thingsInRoom.addElement(new Thing("Sword", "A sword encrusted with jewels"));
- thingsInRoom.addElement(new Thing("Troll", "An ugly, fierce-looking troll"));
- thingsInRoom.addElement(new Thing("Lantern", "A brass lantern"));
- thingsInRoom.addElement(new Thing("Pot of Noodles", "Something inedible-looking"));
- thingsInRoom.addElement(new Thing("Banana", "An over-ripe banana"));
- thingsInRoom.addElement(new Thing("Egg", "An Ostrich egg"));
- thingsInRoom.addElement(new Thing("Knife", "A blood-stained dagger"));
-
- // Give the player some Things too
- thingsInSwagBag.addElement(new Thing("Coin", "A silver coin"));
- thingsInSwagBag.addElement(new Thing("Bubble-gum", "A pink, pre-chewed, stickly lump"));
- thingsInSwagBag.addElement(new Thing("Fluff", "Some pocket fluff"));
-
- // Show the items in List box(es) on screen
- UpdateListBoxes();
- }
-
- public boolean handleEvent(Event event) {
- if (event.id == Event.WINDOW_DESTROY) {
- hide(); // hide the Frame
- dispose();
- System.exit(0);
- return true;
- }
- if (event.target == RoomList && event.id == Event.ACTION_EVENT) {
- RoomList_DblClick(event);
- }
- if (event.target == PlayerList && event.id == Event.ACTION_EVENT) {
- PlayerList_DblClick(event);
- }
- if (event.target == RoomList && event.id == Event.LIST_SELECT) {
- RoomList_ListSelect(event);
- }
- if (event.target == PlayerList && event.id == Event.LIST_SELECT) {
- PlayerList_ListSelect(event);
- }
- return super.handleEvent(event);
- }
-
- public boolean action(Event event, Object arg) {
- if (event.target instanceof MenuItem) {
- String label = (String) arg;
- if (label.equalsIgnoreCase("Open...")) {
- Open_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("About")) {
- About_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("Exit")) {
- Exit_Action(event);
- return true;
- }
- }
- return super.action(event, arg);
- }
-
- static public void main(String args[]) {
- (new Frame1()).show();
- }
-
- //{{DECLARE_CONTROLS
- java.awt.FileDialog OpenFileDialog;
- java.awt.List RoomList;
- java.awt.List PlayerList;
- java.awt.Label label1;
- java.awt.Label label2;
- java.awt.Label label3;
- java.awt.Label label4;
- java.awt.TextField textField1;
- //}}
-
- //{{DECLARE_MENUS
- java.awt.MenuBar mainMenuBar;
- java.awt.Menu menu1;
- java.awt.Menu menu2;
- java.awt.Menu menu3;
- //}}
- }
-
-
- // class Thing
- // any object that has a name and a description
- class Thing {
- private String name;
- private String description;
-
- Thing( String aName, String aDescription) {
- this.name = aName;
- this.description = aDescription;
- }
-
- String getName() {
- return name;
- }
-
- void setName(String aName) {
- this.name = aName;
- }
-
- String getDescription() {
- return description;
- }
-
- void setDescription(String aDescription) {
- this.description = aDescription;
- }
- } // END: class Thing
-